(function() {

	nsbase.views.help = new HelpFunc();

	function HelpFunc() {
		var _self = this;

		_self.init = init;

		var helpActions = nsbase.config.actionUrl.help,
			utilities = nsbase.utility,
			validations = nsbase.utility.validations;

		var cssClass = {
			selected:"selected",
			error:"error"
		};
		var $txtSearch = $("#txtSearch"),
			$btnSearch = $("#btnSearch");
		function init() {
      		$(".nav_menu li.Help").addClass(cssClass.selected);
			bindControlHandler();
			$(".item_box:first").click();
		}

		function bindControlHandler () {
			$(".item_box").click(function(){
				var sender = $(this);
				var $parentContainer = sender.parent("li");
				if($parentContainer.hasClass(cssClass.selected)){
					return false;
				}

				var $helpTypeContainer = $(".help_type");
				$helpTypeContainer.find("li").removeClass(cssClass.selected);
				$parentContainer.addClass(cssClass.selected);
				var helpType = sender.attr("help-type");
				var $curTypeContainer = $("#"+helpType);
				if($curTypeContainer.length>0){
					$(".help_main .help_list ul").hide();
					$curTypeContainer.show();
				}

			});

			$txtSearch.focus(function(){
				var sender = $(this);
				var defaultValue = sender.attr("default-value");
				var value = sender.val();
				if(value == defaultValue){
					sender.val("");
				}
			}).blur(function(){
				var sender = $(this);
				var defaultValue = sender.attr("default-value");
				var value = utilities.trim(sender.val());
				if(!value){
					sender.val(defaultValue);
				}
			});

			$btnSearch.click(function(){
				var searchValue = utilities.trim($txtSearch.val());
				if(!searchValue){
					return;
				}
				var $mainContainer = $(".help_main"),
					$searchContainer = $("#searchContainer"),
					$itemContainer = $searchContainer.find("ul"),
					$listTmpl = $("#listTmpl");
				$mainContainer.hide();
				$itemContainer.empty();
				$searchContainer.show();
				$searchContainer.mask();
				var param = {
					question:searchValue
				};

				nsbase.ajax.request(helpActions.search,param,function(result){
					$searchContainer.unmask();
					if(result.success){
						if(!result.data || result.data.length == 0){
							return;
						}
						$.each(result.data,function(index,questionInfo){
							var $itemTemp = $listTmpl.clone();
							var $itemObj = $itemTemp.find("li");
							if(index%2 == 0){
								$itemObj.addClass("odd");
							} else {
								$itemObj.addClass("even");
							}
							$itemObj.find(".list_order").text(index+1);
							$itemObj.find("h1").text(questionInfo.question);
							$itemObj.append(questionInfo.answer);
							$itemContainer.append($itemTemp.html());
						});
					}
				},function(){
					$searchContainer.unmask();
				});
			});
		}


	}
})();